@extends('backstage.layout.master')
@section('title', '产品列表')
@section('style')
    <style>
        .modal-dialog {
            right: auto;
            left: 50%;
            width: 60%;
            padding-top: 30px;
            padding-bottom: 30px;
        }
    </style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="/backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/product-temp/product-temp-list">产品模板列表</a>
            </li>
            <li>
                产品模板列表
            </li>
        </ul><!-- .breadcrumb -->

        <div class="nav-search" id="nav-search">
        </div><!-- #nav-search -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                产品
                <small>
                    <i class="icon-double-angle-right"></i>
                    产品模板列表
                </small>
                <span class="pull-right">
                    <button class="btn btn-primary" onclick="javascript:window.location.href='{{asset('')}}backstage/product-temp/add-product-temp';">添加产品模板</button>
                </span>
            </h1>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <h3 class="header smaller lighter red">产品模板简介：</h3>
                <div class="well">
                    <h4 class="green smaller lighter">产品模板参数：</h4>
                    1、产品模板Id：{{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:""}}</br>
                    2、品牌：{{isset($product_temp->brand_name)?$product_temp->brand_name:""}}</br>
                    3、产品模板名称：{{isset($product_temp->name)?$product_temp->name:""}}</br>
                    4、产品模板简介：{{isset($product_temp->description)?$product_temp->description:""}}</br>
                    5、价格修改需要凌晨才能生效</br>
                </div>
            </div>
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <input type="hidden" id="product_temp_id" value="{{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:""}}">
                        <thead>
                        <tr>
                            <th class="center">
                                <label>
                                    <input type="checkbox" class="ace">
                                    <span class="lbl"></span>
                                </label>
                            </th>
                            <th>产品编号</th>
                            <th>产品模板编号</th>
                            <th>颜色</th>
                            <th>定价</th>
                            <th>未生效定价</th>
                            <th>排序</th>
                            <th>设置库存</th>
                            <th>更新时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>

                        <tbody>
                        @foreach ($temp_colours as $temp_colour)
                        <tr class="" data-tr-id="{{isset($temp_colour->id)?$temp_colour->id:0}}">
                            <td class="center">
                                <label>
                                    <input type="checkbox" class="ace">
                                    <span class="lbl"></span>
                                </label>
                            </td>
                            <td>
                                {{isset($temp_colour->id)?$temp_colour->id:''}}
                            </td>
                            <td class="product_temp_id">
                                {{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:''}}
                            </td>
                            <td>
                                {{isset($temp_colour->colour_name)?$temp_colour->colour_name:''}}
                            </td>
                            <td style="width:68px;">
                                <input type="number" step=0.01 min=0 name="original_price" value="{{isset($temp_colour->original_price)?$temp_colour->original_price:0}}" class="original_price" style="width:68px;">
                                <input type="hidden" value="{{isset($temp_colour->original_price)?$temp_colour->original_price:0}}">
                                <input type="hidden" value="{{isset($temp_colour->id)?$temp_colour->id:0}}">
                            </td>
                            <td >
                                @if(isset($temp_colour->has_modify_price)&&($temp_colour->has_modify_price==true))
                                    {{isset($temp_colour->modify_price)?$temp_colour->modify_price:''}}
                                 @else
                                   暂无
                                 @endif
                            </td>
                            <td style="width:68px;">
                                <input type="number" min=0 name="sort" value="{{isset($temp_colour->sort)?$temp_colour->sort:0}}" class="sort" style="width:68px;">
                                <input type="hidden" value="{{isset($temp_colour->sort)?$temp_colour->sort:0}}">
                                <input type="hidden" value="{{isset($temp_colour->id)?$temp_colour->id:0}}">
                            </td>
                            <td>
                                <a href="javascript:void(0) ;" class="setStock" data-product-id="{{$temp_colour->id}}">库存设置</a>
                            </td>
                            <td>
                                {{isset($temp_colour->updated_at)?date('Y-m-d H:i:s',$temp_colour->updated_at):''}}
                            </td>
                            <td>
                                <ul class="nav-pills" style="list-style-type:none">
                                    <li class="dropdown all-camera-dropdown">
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <span >操作</span>
                                            <b class="caret"></b></a><ul class="dropdown-menu">
                                            <li data-filter-camera-type="Zed">
                                                <a target="_blank" href="/backstage/product-temp/custom-zone-list-page?product_temp_id={{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:''}}&colour_id={{isset($temp_colour->colour_id)?$temp_colour->colour_id:0}}">
                                                    <span id="">设计区域列表</span>
                                                </a>
                                            </li>
                                            <li data-filter-camera-type="all">
                                                <a  data-delete="{{isset($temp_colour->id)?$temp_colour->id:0}}">
                                                    <span >删除</span>
                                                </a>
                                            </li>
                                        </ul>
                                    </li>
                                </ul>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            <?php echo $temp_colours->render(); ?>
                        </div>
                    </div>
                </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script src="js/template.js"></script>
    <script id="setStockTemplate" type="text/html">
        <table class="group ui-widget ui-widget-content" style="border:0px none;">
            <tbody>
            <tr>
                <td colspan="3">产品颜色ID:@{{prod_color_id}}</td>
            </tr>
            @{{each sizes as value i}}
            <tr>
                <td class="first">
                    尺码：@{{value.size}}
                </td>
                <td class="data">
                    <input type="number" id="stock_@{{i}}"  data-size-name="@{{value.size}}" data-colour-id="@{{value.colour_id}}" data-temp-id="@{{value.product_temp_id}}" data-temp-stock-id="@{{value.temp_stock_id}}" role="textbox" class="input-elm" placeholder="库存" value="@{{value.stock}}" style="width: 80%;">
                </td>
                <td>
                    <button class="btn btn-sm btn-primary" data-id="stock_@{{i}}" onclick="saveStock(this);">虚拟库存保存</button>
                </td>
                <td class="data">
                    <input type="number" id="actual_stock_@{{i}}"  data-size-name="@{{value.size}}" data-colour-id="@{{value.colour_id}}" data-temp-id="@{{value.product_temp_id}}" data-temp-stock-id="@{{value.temp_stock_id}}" role="textbox" class="input-elm" placeholder="库存" value="@{{value.actual_stock}}" style="width: 80%;">
                </td>
                <td>
                    <button class="btn btn-sm btn-primary" data-id="actual_stock_@{{i}}" onclick="saveActualStock(this);">实际库存保存</button>
                </td>
                <td class="data">
                    <input type="text" id="sku_@{{i}}" data-size-name="@{{value.size}}" data-temp-stock-id="@{{value.temp_stock_id}}" data-colour-id="@{{value.colour_id}}" data-temp-id="@{{value.product_temp_id}}" role="textbox" class="input-elm" placeholder="SKU" value="@{{value.sku}}" style="width: 80%;">
                </td>
                <td>
                    <button class="btn btn-sm btn-primary" data-id="sku_@{{i}}" onclick="saveSku(this);">SKU保存</button>
                </td>
            </tr>
            @{{/each}}
            </tbody>
        </table>
    </script>
    <script type="text/javascript">
        $(function () {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $('[data-rel=tooltip]').tooltip();
            $('[data-delete]').click(function(){
                var  temp_colour_id = $(this).attr("data-delete");
                console.log('产品模板颜色删除：'+temp_colour_id);
                if(temp_colour_id!=0) {
                    swal({
                        title: '确定删除该颜色模板？！',
                        text: "",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        closeOnConfirm: true,
                        closeOnCancel: true
                    }, function () {
                        $.ajax({
                            type: "post",
                            url: "/backstage/product-temp/del-product-temp-colour",
                            data: {
                                id: temp_colour_id,
                                _token: "{{csrf_token()}}"
                            },
                            success: function (res) {
                                if (res.state == 1) {
                                    swal({
                                        title: "删除成功！",
                                        text: "",
                                        type: "success",
                                        timer: 2000
                                    });
                                    $('[data-tr-id=' + temp_colour_id + ']').remove();
                                } else {
                                    swal({
                                        title: res.message,
                                        text: "",
                                        type: "error",
                                        timer: 1000
                                    });
                                }
                            },
                            error: function () {
                                swal({
                                    title: "系统繁忙！",
                                    text: "",
                                    type: "error",
                                    timer: 3000
                                });
                            }
                        });
                    });
                }
            });
            $(".sort").blur(function(){
                var sort = $(this).next().val();
                var newSort= $(this).val();
                var id = $(this).next().next().val();
                var a = $(this);
                if(!isInteger(newSort)){
                    swal({
                        title: "请填入正整数!",
                        text: "",
                        type: "error",
                        timer:2000
                    });
                }else if(sort == newSort){
                    return;
                }else{
                    $.ajax({
                        type: "POST",
                        url: "/backstage/product-temp/update-product-temp-colour-sort",
                        data:{"sort":newSort,"id":id},
                        success: function(msg){
                            if(msg.state==1){
                                swal({
                                    title: "更新成功",
                                    text: "",
                                    type: "success",
                                    timer:2000
                                });
                            }else {
                                swal({
                                    title: msg.message,
                                    text: "",
                                    type: "error",
                                    timer:2000
                                });
                            }
                        },
                        error:function(){
                            swal({
                                title: "系统繁忙！",
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    });
                }
            });
            $(".original_price").blur(function(){
                var price = $(this).next().val();
                var newPrice= $(this).val();
                var id = $(this).next().next().val();
                var a = $(this);
                if(!checkDecimals(newPrice)){
                    swal({
                        title: "金额格式不正确!!！",
                        text: "",
                        type: "error",
                        timer:2000
                    });
                }else if(price == newPrice){
                    return;
                }else{
                    $.ajax({
                        type: "POST",
                        url: "/backstage/product-temp/update-price",
                        data:{"price":newPrice,"id":id},
                        success: function(msg){
                            if(msg.state==1){
                                swal({
                                    title: "更新成功",
                                    text: "",
                                    type: "success",
                                    timer:600
                                });
                                location.reload();
                                return ;
                            }else {
                                swal({
                                    title: msg.message,
                                    text: "",
                                    type: "error",
                                    timer:600
                                });
                            }
                        },
                        error:function(){
                            swal({
                                title: "系统繁忙！",
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    });
                }
            });
            $('table th input:checkbox').on('click' , function(){
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                        .each(function(){
                            this.checked = that.checked;
                            $(this).closest('tr').toggleClass('selected');
                        });

            });
            var set_stock_state=true;
            $(".setStock").click(function(){
                var mythis = $(this);
                var product_temp_id=$('#product_temp_id').val();
                mythis.text("加载中。。");
                var prod_color_id = mythis.attr("data-product-id");
                if(!set_stock_state){
                    return ;
                }
                set_stock_state=false;
                $.post("/backstage/product-temp/get-stock-by-colour-id",
                    {
                        id:prod_color_id,
                        product_temp_id:product_temp_id,
                        _token:"{{csrf_token()}}"
                    },
                    function(data,status){
                        if(data.state ==1)
                        {
                            var data = {
                                prod_color_id:prod_color_id,
                                product_temp_id:product_temp_id,
                                sizes:data.data,
                            };
                            var html = template('setStockTemplate', data);
                            bootbox.alert(html);

                        }else{
                            bootbox.alert(data.message);
                        }
                        mythis.text("库存设置");
                    });
                set_stock_state=true;
            });
        });
        /***
         * 小数格式校验
         * @param num
         * @returns {boolean}
         */
        function checkDecimals(num)
        {
            var reg = /^\d+(?=\.{0,1}\d+$|$)/
            return reg.test(num);
        }
        /**
         * 判断是不是整数
         * */
        function isInteger(num) {
            var reg= /^[0-9]*[1-9][0-9]*$/;
            return reg.test(num);
        }
        /***
         * 库存保存
         * @param mythis
         */
        function saveStock(mythis){
            var btn =  $(mythis);
            btn.removeClass("btn-primary");
            btn.addClass("btn-warning");
            btn.text("等待");
            var id = btn.attr("data-id");
            var size_name = $("#"+id).attr("data-size-name");
            var colour_id = $("#"+id).attr("data-colour-id");
            var temp_id = $("#"+id).attr("data-temp-id");
            var temp_stock_id = $("#"+id).attr("data-temp-stock-id");
            var stock = $("#"+id).val();
            $.post("/backstage/product-temp/save-product-temp-stock",
                {
                    temp_stock_id:temp_stock_id,
                    colour_id:colour_id,
                    temp_id:temp_id,
                    stock:stock,
                    size_name:size_name,
                    _token:"{{csrf_token()}}"
                },
                function(data,status){
                    if(data.state!=1)
                    {
                        bootbox.alert(data.message);
                    }
                    btn.removeClass("btn-warning");
                    btn.addClass("btn-primary");
                    btn.text("虚拟库存保存");
                });
        }
        function saveActualStock(mythis){
            var btn =  $(mythis);
            btn.removeClass("btn-primary");
            btn.addClass("btn-warning");
            btn.text("等待");
            var id = btn.attr("data-id");
            var size_name = $("#"+id).attr("data-size-name");
            var colour_id = $("#"+id).attr("data-colour-id");
            var temp_id = $("#"+id).attr("data-temp-id");
            var temp_stock_id = $("#"+id).attr("data-temp-stock-id");
            var actual_stock = $("#"+id).val();
            $.post("/backstage/product-temp/save-product-temp-actual-stock",
                {
                    temp_stock_id:temp_stock_id,
                    colour_id:colour_id,
                    temp_id:temp_id,
                    actual_stock:actual_stock,
                    size_name:size_name,
                    _token:"{{csrf_token()}}"
                },
                function(data,status){
                    if(data.state!=1)
                    {
                        bootbox.alert(data.message);
                    }
                    btn.removeClass("btn-warning");
                    btn.addClass("btn-primary");
                    btn.text("实际库存保存");
                });
        }
        /***
         * Sku保存
         * @param mythis
         */
        function saveSku(mythis){
            var btn =  $(mythis);
            btn.removeClass("btn-primary");
            btn.addClass("btn-warning");
            btn.text("等待");
            var id = btn.attr("data-id");
            var size_name = $("#"+id).attr("data-size-name");
            var temp_stock_id = $("#"+id).attr("data-temp-stock-id");
            var colour_id = $("#"+id).attr("data-colour-id");
            var temp_id = $("#"+id).attr("data-temp-id");
            var sku = $("#"+id).val();
            $.post("/backstage/product-temp/save-product-temp-sku",
                {
                    temp_stock_id:temp_stock_id,
                    sku:sku,
                    size_name:size_name,
                    colour_id:colour_id,
                    temp_id:temp_id,
                    _token:"{{csrf_token()}}"
                },
                function(data,status){
                    if(data.state!=1)
                    {
                        bootbox.alert(data.message);
                    }else {
                        bootbox.alert('保存成功');
                    }
                    btn.removeClass("btn-warning");
                    btn.addClass("btn-primary");
                    btn.text("SKU保存");
                });
        }
    </script>
@stop